package com.logging.service;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.logging.entity.HttpLogEntry;

@Service
public class LoggingService extends HibernateDaoSupport implements ILoggingService {
	private static Logger log = Logger.getLogger(LoggingService.class);
	
	@Autowired
	public void setSessionFactoryOverride(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	@Transactional
	@Override
	public void saveAllLogs(List<? extends HttpLogEntry> httpLogs) {
		Session session = this.getSessionFactory().getCurrentSession();
		Date now = new Date();
		for(HttpLogEntry logEntry : httpLogs){
			logEntry.setCreateDate(now);
			log.info(logEntry);
			session.save(logEntry);
		}
	}
}
